﻿using System.ComponentModel;
using System.Drawing.Design;
using System.Web;
using System.Web.UI;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Summary description for Image
	/// </summary>
	public class Image : BaseSimpleValueControl, IImageControl
	{
		#region STATIC
		/// <summary>
		/// Resolves an image URL by recognizing "~ti/" prefix for the current theme's images folder and "~/" for application root.
		/// </summary>
		/// <param name="url"></param>
		/// <returns></returns>
		public static string ResolveSrc(string url)
		{
			if (string.IsNullOrEmpty(url))
				return null;
			if (HttpContext.Current == null || !(HttpContext.Current.Handler is Page))
				return url;
			return ((Page)HttpContext.Current.Handler).ResolveUrl(url.Replace("~ti/", string.Format("~/App_Themes/{0}/Images/", ((Page)HttpContext.Current.Handler).Theme)));
		}
		#endregion
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public Image()
		{
			//
			// TODO: Add constructor logic here
			//
			Tag = HtmlTextWriterTag.Img;

		}
		#endregion
		#region PROPS
		#region DEF
		/// <summary>
		/// Props.
		/// </summary>
		protected new static Properties _clientProperties = Define(BaseValueDisplayControl._clientProperties, ImageControlCommon.Properties);
		/// <summary>
		/// Props.
		/// </summary>
		public override Properties ClientProperties
		{
			get
			{
				return _clientProperties;
			}
		}
		#endregion
		[DefaultValue(HtmlTextWriterTag.Img)]
		public override HtmlTextWriterTag Tag
		{
			get
			{
				return base.Tag;
			}
			set
			{
				base.Tag = value;
			}
		}
		/// <summary>
		/// The URL of the image to be displayed.
		/// </summary>
		[Editor("System.Web.UI.Design.ImageUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
		[UrlProperty]
		public string Src { get { return this.GetProperty<string>("Src"); } set { this.ViewState["Src"] = value; } }
		/// <summary>
		/// The alternate text to be display when images not supported or disabled.
		/// </summary>
		public string Alt { get { return this.GetProperty<string>("Alt"); } set { this.ViewState["Alt"] = value; } }
		/// <summary>
		/// Non-typed value object.
		/// </summary>
		public override object GenericValue
		{
			get
			{
				return this.Src;
			}
			set
			{
				this.Src = (string)value;
			}
		}
		#endregion
		#endregion
		#region INTERNAL
		#region PROPS
		/// <summary>
		/// Gets or sets the <see cref="Src"/> property.
		/// </summary>
		protected internal override string SerializedValue
		{
			get
			{
				return ResolveSrc(Src);
			}
			set
			{
				Src = value;
			}
		}
		#endregion
		#region METHODS
		#region INIT
		/// <summary>
		/// Registers ImageControl.js.
		/// </summary>
		protected internal override void RegisterIncludes(IncludeCollection includes)
		{
			base.RegisterIncludes(includes);

			includes.Add(new ScriptResourceInclude("TA.Turbo.Res.JS.Simple.ImageControl.js", typeof(Image)));
		}
		#endregion
		#region RENDER
		/// <summary>
		/// Adds the "src" attribute.
		/// </summary>
		/// <param name="writer"></param>
		protected override void AddValueAttributes(HtmlTextWriter writer)
		{
			writer.AddAttribute(HtmlTextWriterAttribute.Src, Image.ResolveSrc(Src));
		}
		/// <summary>
		/// Adds the "alt" attribute.
		/// </summary>
		/// <param name="writer"></param>
		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			if (!string.IsNullOrEmpty(Alt))
				writer.AddAttribute(HtmlTextWriterAttribute.Alt, Alt);
			base.AddAttributesToRender(writer);

		}
		#endregion
		#endregion
		#endregion
	}
}